package com.kaigejava.juc.cas;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.concurrent.atomic.AtomicReference;

/**
 * @author kaigejava
 * 原子引用 测试
 */
public class AtomicReferenceTest {
    public static void main(String[] args) {
        AtomicUser user1 = new AtomicUser(24,"刘亦菲");
        AtomicUser user2 = new AtomicUser(24,"林依晨");
        AtomicUser user3 = new AtomicUser(24,"佟丽娅");
        AtomicReference<AtomicUser> atomicReference  = new  AtomicReference<AtomicUser>();
        //X年之前是刘亦菲
        atomicReference.set(user1);
        //X+N年之后林依晨
        System.out.println(atomicReference.compareAndSet(user1,user2)+"\t ===心中女神："+atomicReference.get().toString());
        //现在佟丽娅
        System.out.println( atomicReference.compareAndSet(user2,user3)+"\t ===心中女神："+atomicReference.get().toString());
    }
}


/**
 * @author kaigejava
 * 用户对象
 */
@Data
@AllArgsConstructor
class AtomicUser{
    private  int age;
    private String name;
}